use std::sync::Arc;
use serde_json::json;
use hex_literal::hex;
use sha1::{Sha1, Digest};

pub fn createrepo(owner:&String,repo:&String,token:&String){
    let url = "https://gitee.com/api/v5/user/repos";

    let data = json!({
        "access_token": token,
        "name": repo,
        "has_issues": true,
        "has_wiki": true,
        "can_comment": true,
        "auto_init":true,
        "private": true,
    });
    loop{
        let client = reqwest::blocking::Client::new();
        let response = client.post(url)
        .header("Content-Type", "application/json;charset=UTF-8")
        .json(&data)
        .send();
        match response{
            Ok(res)=>{
                if res.status() == 400{//400 retry
                    continue;
                }
                break;
            }
            Err(err)=>{
                continue;
            }
        }
    }
}
pub fn delrepo(owner:&String,repo:&String,token:&String){
    let url = unsafe{"https://gitee.com/api/v5/repos/".to_owned()+&*owner+"/"+&*repo};
    loop{
        let client = reqwest::blocking::Client::new();
        let response = client.delete(&url)
            .header("Authorization", "Bearer ".to_owned()+&token)
            .send();
        match response{
            Ok(res)=>{
                if res.status() == 400{//400 retry
                    continue;
                }
                break;
            }
            Err(err)=>{
                continue;
            }
        }
    }
}
pub fn publicrepo(owner:&String,repo:&String,token:&String){
    let url = "https://gitee.com/api/v5/repos/".to_owned()+&owner+"/"+&repo;
    let data = json!({
        "access_token": token,
        "name": repo,
        "owner":owner,
        "repo":repo,
        "private": false,
    });
    loop{
        let client = reqwest::blocking::Client::new();
        let response = client.patch(&url)
            .header("Authorization", "Bearer ".to_owned()+&token)
            .json(&data)
            .send();
        match response{
            Ok(res)=>{
                if res.status() == 400{//400 retry
                    continue;
                }
                break;
            }
            Err(err)=>{
                continue;
            }
        }
    }
}
